﻿#pragma once
#include <iostream>
#include <string>
#include <assert.h>
using namespace std;
namespace MySpace
{
	//时 hours, 分 minutes, 秒 seconds
	class Time
	{
	public:
		//获得时间
		char* GetTime()
		{
			int hhigh = _hours / 10;
			int hlow = _hours % 10;
			_thistime[0] = hhigh + '0';
			_thistime[1] = hlow + '0';

			int mhigh = _minutes / 10;
			int mlow = _minutes % 10;
			_thistime[4] = mhigh + '0';
			_thistime[5] = mlow + '0';

			int shigh = _seconds / 10;
			int slow = _seconds % 10;
			_thistime[8] = shigh + '0';
			_thistime[9] = slow + '0';

			_thistime[10] = '\0';
			return _thistime;
		}
		//构造函数
		Time(int hours = 0, int minutes = 0, int seconds = 0)
			:_hours(hours)
			, _minutes(minutes)
			, _seconds(seconds)
		{
			if (_hours < 0 || _hours >= 24)_hours = 0;
			if (_minutes < 0 || _minutes >= 60)_minutes = 0;
			if (_seconds < 0 || _seconds >= 60)_seconds = 0;
			_thistime = new char[12];
			_thistime[2] = ':';
			_thistime[3] = ':';
			_thistime[6] = ':';
			_thistime[7] = ':';
			GetTime();
			cout << this << "  " << &_thistime << "  构造函数" << endl;
		}
		//拷贝构造
		Time(const Time& copytime)
		{
			this->_hours = copytime._hours;
			this->_minutes = copytime._minutes;
			this->_seconds = copytime._seconds;
			this->_thistime = new char[12];
			this->_thistime[2] = ':';
			this->_thistime[3] = ':';
			this->_thistime[6] = ':';
			this->_thistime[7] = ':';
			this->GetTime();
			cout << this << "  " << &_thistime << "  拷贝构造" << endl;
		}
		//析构函数
		~Time()
		{
			delete[](_thistime);
			_thistime = NULL;
			cout << this << "  " << &_thistime << "  析构函数" << endl;
		}
		//获得小时
		int GetHours()
		{
			return _hours;
		}
		//设置小时
		void SetHours(int hours = 0)
		{
			if (hours >= 0 && hours < 24)_hours = hours;
		}
		//获得分钟
		int GetMinutes()
		{
			return _minutes;
		}
		//设置分钟
		void SetMinutes(int minutes = 0)
		{
			if (minutes >= 0 && minutes < 60)_minutes = minutes;
		}
		//获得秒钟
		int GetSeconds()
		{
			return _seconds;
		}
		//设置秒钟
		void SetSeconds(int seconds = 0)
		{
			if (seconds >= 0 && seconds < 60)_seconds = seconds;
		}

		//打印
		void PrintData()const
		{
			cout << _hours << " " << _minutes << " " << _seconds << endl;
		}

		//类加时
		Time& AddHou(int hours)
		{
			int hou = hours + _hours;
			hou %= 24;
			_hours = hou;
			return *this;
		}

		//类加分
		Time& AddMin(int minutes)
		{
			int min = minutes + _minutes;
			int hou = min / 60;
			min %= 60;
			_minutes = min;
			AddHou(hou);
			return *this;
		}

		//类加秒
		Time& AddSec(int seconds)
		{
			int sec = seconds + _seconds;
			int min = sec / 60;
			sec %= 60;
			_seconds = sec;
			AddMin(min);
			return *this;
		}

		//运算符重载
		Time& operator+=(Time& optime)
		{
			AddSec(optime.GetSeconds());
			AddMin(optime.GetMinutes());
			AddHou(optime.GetHours());
			return *this;
		}

		//运算符重载
		Time operator+(Time& time)
		{
			Time newtime(*this);
			newtime += time;
			return newtime;
		}
	private:
		int _hours = 0;
		int _minutes = 0;
		int _seconds = 0;
		//public:
		char* _thistime;
	protected:
	};



	//年 year, 月 month, 日 day
	class Date
	{
		//friend void operator <<(ostream& out, const Date& date);
		//friend void operator >>(istream& in,Date& date);

	public:

		int GatMonthDay(int year, int month)
		{
			assert(month > 0 && month < 13);
			// 月数组
			static int monthDayArray[13] = { -1, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
			// 润年判断
			if (month == 2 && (year % 4 == 0 && year % 100 != 0) || (year
				% 400 == 0))
			{
				return 29;
			}
			else
			{
				return monthDayArray[month];
			}
		}
		//构造函数
		Date(int year = 1970, int month = 1, int day = 1)
			:_year(year)
			, _month(month)
			, _day(day)
		{
			SetYear(year);
			SetMonth(month);
			SetDay(day);
			//cout << this << "构造函数" << endl;
		}
		//拷贝构造
		Date(const Date& copydate)
		{
			this->_year = copydate._year;
			this->_month = copydate._month;
			this->_day = copydate._day;
			//cout << this << "拷贝构造" << endl;
		}
		//赋值重载
		Date& operator=(const Date& copydate)
		{
			_year = copydate._year;
			_month = copydate._month;
			_day = copydate._day;
			return *this;
		}
		//取地址重载
		Date* operator&()
		{
			return this;
		}

		//析构函数
		~Date()
		{
			//cout << this << "析构函数" << endl;
		}
		//获得年
		int GetYear()const
		{
			return _year;
		}
		//设置年
		void SetYear(int year = 1970)
		{
			assert(year >= 0 && year < 5000);
			_year = year;
		}
		//获得月
		int GetMonth()const
		{
			return _month;
		}
		//设置月
		void SetMonth(int month = 1)
		{
			assert(month >= 1 && month <= 12);
			_month = month;
		}
		//获得日
		int GetDay()const
		{
			return _day;
		}
		//设置日
		void SetDay(int day = 1)
		{
			int maxday = GatMonthDay(_year, _month);
			assert(day >= 1 && day <= maxday);
			_day = day;
		}

		//打印
		void PrintDate()const
		{
			cout << _year << " " << _month << " " << _day << endl;
		}

		bool CheckDate()
		{
			if (_year >= 0 && _year < 5000)
			{
				if (_month >= 1 && _month <= 12)
				{
					int maxday = GatMonthDay(_year, _month);
					if (_day >= 1 && _day <= maxday)
					{
						return true;
					}
				}
			}
			return false;
		}

		//类加年
		Date& AddYear(int year)
		{
			_year += year;
			if (_year > 5000)_year = 5000;
			return *this;
		}

		//类加月
		Date& AddMonth(int month)
		{
			int mon = month + _month;
			int ye = ye / 12;
			mon %= 12;
			_month = mon;
			AddYear(ye);
			SetDay(_day);
			return *this;
		}

		//类加日
		Date& AddDay(int day)
		{
			_day += day;
			while (!CheckDate())
			{
				int month_day = GatMonthDay(_year, _month);
				if (_day > month_day)
				{
					_month++;
					if (_month > 12)
					{
						_year++;
						_month = 1;
					}
					_day -= month_day;
				}
			}
			return *this;
		}

		//类减年
		Date& SubYear(int year)
		{
			_year -= year;
			if (_year <= 0)_year = 0;
			return *this;
		}

		//类减月
		Date& SubMonth(int month)
		{
			_month -= month;
			while (_month <= 1)
			{
				_month -= 12;
				--_year;
			}
			SubYear(0);
			return *this;
		}

		//类减日
		Date& SubDay(int day)
		{
			_day -= day;
			while (!CheckDate())
			{
				if (_day <= 0)
				{
					_month--;
					if (_month <= 0)
					{
						_year--;
						_month = 12;
					}
					int month_day = GatMonthDay(_year, _month);
					_day += month_day;
				}
			}
			return *this;
		}

		bool operator<(const Date& d) const;
		bool operator<=(const Date& d) const;
		bool operator>(const Date& d) const;
		bool operator>=(const Date& d) const;
		bool operator==(const Date& d) const;
		bool operator!=(const Date& d) const;
		// d1 += 天数
		Date& operator+=(int day);
		Date operator+(int day) const;
		// d1 -= 天数
		Date& operator-=(int day);
		Date operator-(int day) const;
		// d1 - d2
		int operator-(const Date& d) const;
		// ++d1 -> d1.operator++()
		Date& operator++();
		// d1++ -> d1.operator++(0)
		// 为了区分，构成重载，给后置++，强⾏增加了⼀个int形参
		// 这⾥不需要写形参名，因为接收值是多少不重要，也不需要⽤
		// 这个参数仅仅是为了跟前置++构成重载区分
		Date operator++(int);
		Date& operator--();
		Date operator--(int);
		// 流插⼊
		//// 不建议，因为Date* this占据了⼀个参数位置，使⽤d<<cout不符合习惯
		//ostream& operator <<(ostream& out)
		//{
		//	out << _year << "年" << _month << "月" << _day << "日" << endl;
		//	return out;
		//}
	private:
		int _year = 1970;
		int _month = 1;
		int _day = 1;
	protected:

	};
}